package com.dadibadi.core.user;

import com.dadibadi.core.user.vo.UploadAliPulblicVO;
import com.venusoft.developer.common.exception.BizException;
import com.venusoft.developer.common.result.CommonResult;
import com.venusoft.developer.common.result.ICommonResult;
import com.venusoft.developer.common.security.IBaseUser;
import com.venusoft.developer.obs.service.AliOssService;
import com.venusoft.developer.web.utils.UserHolder;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.media.SchemaProperty;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/api")
@Slf4j
@Tag(name = "api/资源管理", description = "")
public class AdminFileUploaderController {
    @Resource
    AliOssService aliOssService;

    @PostMapping("/api-file-uploader-test")
    @Operation(summary = "文件上传测试", description = "")
    @ApiResponses(value = {@ApiResponse(responseCode = "200", content = {@Content(schema = @Schema(implementation = UploadAliPulblicVO.class), mediaType = "application/json")})})
    public ICommonResult exec(
            @RequestPart(required = true)
            @io.swagger.v3.oas.annotations.parameters.RequestBody(
                    description = "文件",
                    content = @Content(
                            mediaType = "multipart/form-data",
                            schema = @Schema(type = "object"),
                            schemaProperties = {
                                    @SchemaProperty(
                                            name = "file",
                                            schema = @Schema(type = "file", description = "文件", format = "binary")
                                    )
                            }
                    )
            ) MultipartFile file,
            @RequestParam(required = true) @Parameter(description = "存储桶") String bucketName,
            @RequestParam(required = true) @Parameter(description = "存放文件夹") String dir,
            @RequestHeader(required = false) String platform,
            @RequestHeader(required = false) String version,
            HttpServletRequest request

    ) throws IOException {
        IBaseUser<String> curUser = UserHolder.get(String.class).orElseThrow(() -> BizException.DATA_NOT_FOUND.withMessage("The user does not exists"));
        String bucketNameFinal = aliOssService.getRawBucketName(bucketName);
        String key = aliOssService.generateKey(dir, file.getOriginalFilename());

        log.info("bucketName:{}, key:{}", bucketNameFinal, key, file.getInputStream());
        aliOssService.getOssClient().putObject(bucketNameFinal, key, file.getInputStream());

        UploadAliPulblicVO vo = new UploadAliPulblicVO();

        vo.setKey(key);
        vo.setHost("https://" + bucketName + "." + aliOssService.getEndpoint());
        log.info("public: ok");
        return new CommonResult<>(vo);


    }

}
